package com.wxsoft.business.physic.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wxsoft.business.common.controller.BaseController;
import com.wxsoft.business.model.easyui.DataGrid;
import com.wxsoft.business.model.easyui.PageHelper;
import com.wxsoft.business.physic.pojo.PsPhysicPriceModifyDetail;
import com.wxsoft.business.physic.pojo.PsPhysicPriceModifyHead;
import com.wxsoft.business.physic.service.IPsPhysicPriceModifyService;
import com.wxsoft.business.sys.pojo.SysUser;
import com.wxsoft.util.UserUtil;
import com.wxsoft.util.common.JsonTools;

@Controller
@RequestMapping("/psPhysicPriceModify")
public class PsPhysicPriceModifyController extends BaseController{
	
	private Logger logger = Logger.getLogger(this.getClass());
	
	@Resource
	private IPsPhysicPriceModifyService service;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Model model) {
		return "physic/psPhysicPriceModifyHeadlist";
	}
	
    @ResponseBody
	@RequestMapping("/datagrid")
	public DataGrid list(HttpServletRequest request,PageHelper page, PsPhysicPriceModifyHead psPhysicPriceModifyHead,HttpServletResponse response) throws IOException{
		
		DataGrid dg = new DataGrid();
		dg.setTotal(service.findCount(psPhysicPriceModifyHead));
		List<PsPhysicPriceModifyHead> psPhysicPriceModifyHeadList = service.findAll(page, psPhysicPriceModifyHead);
		dg.setRows(psPhysicPriceModifyHeadList);

		return dg ;
	}
	
	@ResponseBody
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public void add(HttpServletRequest request,HttpServletResponse response, PsPhysicPriceModifyHead psPhysicPriceModifyHead) {
		try {
			String inserted = request.getParameter("inserted");
			String updated = request.getParameter("updated");
			String deleted = request.getParameter("deleted");
			List<PsPhysicPriceModifyDetail> pppmdForinst = JsonTools.parseArray(inserted, PsPhysicPriceModifyDetail.class);
			List<PsPhysicPriceModifyDetail> pppmdForUpdated = JsonTools.parseArray(updated, PsPhysicPriceModifyDetail.class);
			List<PsPhysicPriceModifyDetail> pppmdForDel = JsonTools.parseArray(deleted, PsPhysicPriceModifyDetail.class);
			SysUser user = UserUtil.getUser(request);
			psPhysicPriceModifyHead.setHandle(user.getUser_id());
			psPhysicPriceModifyHead.setHandle_date(new Date());
			psPhysicPriceModifyHead.setDepartment(user.getUnit_id());
			String r = service.add(psPhysicPriceModifyHead,pppmdForinst,pppmdForUpdated,pppmdForDel);
			this.writeReturn(request,response, r);
		} catch (Exception e) {
			e.printStackTrace();
			this.writeReturn(request,response , false, e.getMessage());
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/modify", method = RequestMethod.POST)
	public void modify(HttpServletRequest request,HttpServletResponse response, PsPhysicPriceModifyHead psPhysicPriceModifyHead) {
		try {
			String r = service.modify(psPhysicPriceModifyHead);
			this.writeReturn(request,response, r);
		} catch (Exception e) {
			e.printStackTrace();
			this.writeReturn(request,response, false, e.getMessage());
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public void delete(HttpServletRequest request,HttpServletResponse response, PsPhysicPriceModifyHead psPhysicPriceModifyHead) {
		try {
			String r = service.delete(psPhysicPriceModifyHead);
			this.writeReturn(request,response, r);
		} catch (Exception e) {
			e.printStackTrace();
			this.writeReturn(request,response, false, e.getMessage());
		}
	}
}
